//
//  CompanyCell.m
//  MedicineBox
//
//  Created by dllo on 15/8/24.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "CompanyCell.h"
#import "UIImageView+WebCache.h"
#import "HomeModel.h"

#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"

@interface CompanyCell ()

@property (nonatomic, assign) int tag;
@property (nonatomic, retain) HomeModel *rightModel;
@property (nonatomic, retain) HomeModel *centerModel;
@property (nonatomic, retain) HomeModel *leftModel;
@property (nonatomic, retain) HomeModel *lastModel;
@property (nonatomic, retain) UIImageView *rightImage;
@property (nonatomic, retain) UIImageView *leftImage;
@property (nonatomic, retain) UIImageView *centerImage;
@property (nonatomic, retain) UIImageView *lastImage;

@end

@implementation CompanyCell

-(void)dealloc
{
    
    [_lastImage release];
    [_rightModel release];
    [_centerModel release];
    [_lastModel release];
    [_leftModel release];
    [_centerImage release];
    [_rightImage release];
    [_leftImage release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
//        if (_rightModel == nil) {
//            
//            self.rightModel = [[HomeModel alloc] init];
//        } if (_centerModel == nil) {
//            
//             self.centerModel = [[HomeModel alloc] init];
//            
//        } if (_leftModel == nil) {
//            
//              self.leftModel = [[HomeModel alloc] init];
//            
//        }
        
        
        [DKNightVersionManager addClassToSet:self.class];
        self.nightBackgroundColor = UIColorFromRGB(0x343434);
        
        self.rightImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.rightImage];
        [_rightImage release];
        _rightImage.userInteractionEnabled = YES;
        UITapGestureRecognizer *rightTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightTapAction:)];
        [_rightImage addGestureRecognizer:rightTap];
        [rightTap release];

        self.leftImage = [[UIImageView alloc] init];
        
        [self.contentView addSubview:self.leftImage];
        [_leftImage release];
        _leftImage.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *leftTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftTapAction:) ];
        [_leftImage addGestureRecognizer:leftTap];
        [leftTap release];
        
        
        self.centerImage = [[UIImageView alloc] init];
        _centerImage.userInteractionEnabled = YES;
        [self.contentView addSubview:self.centerImage];
        [_centerImage release];
        UITapGestureRecognizer *centerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(centerTapAction:)];
        [_centerImage addGestureRecognizer:centerTap];
        [centerTap release];
      
        self.lastImage = [[UIImageView alloc] init];
        _lastImage.userInteractionEnabled = YES;
        [self.contentView addSubview:self.lastImage];
        [_lastImage release];
        UITapGestureRecognizer *lastTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastTapAction:)];
        [_lastImage addGestureRecognizer:lastTap];
        [lastTap release];
        
    }
    return self;
}

- (void)rightTapAction:(UITapGestureRecognizer *)tap
{

    [(NSObject *)self.target performSelector:self.action withObject:_rightModel withObject:[NSString stringWithFormat:@"%d",self.tag]];

}

- (void)centerTapAction:(UITapGestureRecognizer *)tap
{

    [(NSObject *)self.target performSelector:self.action withObject:_centerModel withObject:[NSString stringWithFormat:@"%d",self.tag]];

}

- (void)leftTapAction:(UITapGestureRecognizer *)tap
{

    [(NSObject *)self.target performSelector:self.action withObject:_leftModel withObject:[NSString stringWithFormat:@"%d",self.tag]];

}

- (void)lastTapAction:(UITapGestureRecognizer *)tap
{

    [(NSObject *)self.target performSelector:self.action withObject:_lastModel withObject:[NSString stringWithFormat:@"%d",self.tag]];

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.rightImage.frame = CGRectMake(20, 10, (self.contentView.frame.size.width - 40) / 5, _rightImage.frame.size.width);
    
    self.centerImage.frame = CGRectMake(self.rightImage.frame.size.width + self.rightImage.frame.origin.x + 20, 10, (self.contentView.frame.size.width - 40) / 5, _centerImage.frame.size.width);
   
    
    self.leftImage.frame = CGRectMake(self.centerImage.frame.size.width + self.centerImage.frame.origin.x + 20, 10, (self.contentView.frame.size.width - 40) / 5, _leftImage.frame.size.width);
   
     self.lastImage.frame = CGRectMake(self.leftImage.frame.size.width + self.leftImage.frame.origin.x + 20, 10, (self.contentView.frame.size.width - 40) / 5, _leftImage.frame.size.width);
  
}



-(void)valueWithModel:(HomeModel *)model tag:(int)tag
{
 
    self.tag = tag;
    switch (tag) {
        case 0:
            _rightModel = model;
            [self.rightImage sd_setImageWithURL:[NSURL URLWithString:model.titleimg]];
           [self.rightImage sd_setImageWithURL:[NSURL URLWithString:model.titleimg] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
               self.rightImage.nightImage = image;
           }];
            break;
        case 1:
            _centerModel = model;
            [self.centerImage sd_setImageWithURL:[NSURL URLWithString:model.titleimg]];
            [self.centerImage sd_setImageWithURL:[NSURL URLWithString:model.titleimg] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                self.centerImage.nightImage = image;
            }];
           
        case 2:
            _leftModel = model;
            [self.leftImage sd_setImageWithURL:[NSURL URLWithString:model.titleimg]];
            [self.leftImage sd_setImageWithURL:[NSURL URLWithString:model.titleimg] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                self.leftImage.nightImage = image;
            }];
           
        case 3:
            _lastModel = model;
            [self.lastImage sd_setImageWithURL:[NSURL URLWithString:model.titleimg]];
            [self.lastImage sd_setImageWithURL:[NSURL URLWithString:model.titleimg] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                self.lastImage.nightImage = image;
            }];
        default:
            break;
    }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
